package com.nbgs.demo.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbgs.demo.controller.form.UserNews;
import com.nbgs.demo.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface UserMapper {
    //录入用户信息
    @Insert("insert into t_user(username,ID_num,user_address,user_tel) value(#{username},#{IDNum},#{userAddress},#{userTel})")
    void addUser(User user);
    //通过身份证查出userId
    @Select("select user_id from t_user where ID_num=#{IDNum}")
    int findUserIdByIDNum(String IDNum);
    //动态sql查询信息
    @Select("<script>\n" +
            "SELECT t1.card_num,t2.username,t2.ID_num,t2.user_address,t2.user_tel,t3.meter_state,t3.meter_id,t5.area_name,t4.type,t3.meter_date from \n" +
            "t_card as t1 \n" +
            "LEFT JOIN t_user as t2 ON t1.user_id=t2.user_id\n" +
            "LEFT JOIN t_meter as t3 ON t3.meter_id=t1.meter_id\n" +
            "LEFT JOIN t_type as t4 ON t1.type_id=t4.type_id\n" +
            "LEFT JOIN t_area as t5 on t5.area_num=t3.area_num\n" +
            "<where>\n" +
            "<if test=\"cardNum!=null and cardNum!=''\">\n"+
            "and t1.card_num like CONCAT('%',#{cardNum},'%')\n"+
            "</if>\n" +
            "<if test=\"areaName!=null and areaName!=''\">\n" +
            "and t5.area_name=#{areaName}\n" +
            "</if>\n" +
            "<if test=\"meterState!=null and meterState!=''\">\n" +
            "and t3.meter_state=#{meterState}\n" +
            "</if>\n" +
            "<if test=\"type!=null and type!=''\">\n" +
            "and t4.type=#{type}\n" +
            "</if>\n" +
            "</where>           \n" +
            "ORDER BY card_num DESC\n" +
            "</script>")
       IPage<UserNews> showSumUserNews(Page<UserNews> page, @Param(value = "areaName") String areaName, @Param(value = "type")String type, @Param(value = "meterState")String meterState,@Param(value="cardNum") String cardNum);
    //数据库查询总条数
    @Select("<script>\n" +
            "SELECT count(*) from t_card  as t1\n" +
            "LEFT JOIN t_user as t2 ON t1.user_id=t2.user_id\n" +
            "LEFT JOIN t_meter as t3 ON t3.meter_id=t1.meter_id\n" +
            "LEFT JOIN t_type as t4 ON t1.type_id=t4.type_id\n" +
            "LEFT JOIN t_area as t5 on t5.area_num=t3.area_num\n" +
            "<where>\n" +
            "<if test=\"cardNum!=null and cardNum!=''\">\n"+
            "and t1.card_num=#{cardNum}\n"+
            "</if>\n" +
            "<if test=\"areaName!=null and areaName!=''\">\n" +
            "and t5.area_name=#{areaName}\n" +
            "</if>\n" +
            "<if test=\"meterState!=null and meterState!=''\">\n" +
            "and t3.meter_state=#{meterState}\n" +
            "</if>\n" +
            "<if test=\"type!=null and type!=''\">\n" +
            "and t4.type=#{type}\n" +
            "</if>\n" +
            "</where>           \n" +
            "ORDER BY card_num DESC\n" +
            "</script>")
    int getTotalUserNews( @Param(value = "areaName") String areaName, @Param(value = "type")String type, @Param(value = "meterState")String meterState,@Param(value="cardNum") String cardNum);



    //修改用户信息
    @Update("<script>\n"+
            "update t_user\n" +
            "<set>\n" +
            "<if test=\"userTel!=null and userTel!=''\">\n" +
            " user_tel=#{userTel}\n" +
            "</if>\n" +
            "</set>\n" +
            "where user_id=#{userId}\n"+
            "</script>\n")
    void updateUser(User user);


}


